// destiny.js
Page({
  data: {
    destinyData: null,
    showExplanation: false,
    currentBazi: '',
    baziExplanation: '',
    lastName: '',
    bazi: '',
    wuxing: '',
    xiyongshen: '',
    lianghua: '',
    elementScores: {},
    preferredElement: ''
  },

  onLoad: function(options) {
    const app = getApp();
    
    // 添加空值检查，避免destinyData为null时出错
    if (!app.globalData || !app.globalData.destinyData) {
      console.log('未找到命理数据，使用默认值');
      
      // 设置默认数据
      this.setData({
        lastName: app.globalData?.nameFormData?.surname || '未设置',
        bazi: '暂无八字数据',
        wuxing: '暂无五行数据',
        xiyongshen: '暂无喜用神数据',
        lianghua: '暂无量化数据',
        elementScores: {
          '金': 0,
          '木': 0,
          '水': 0,
          '火': 0,
          '土': 0
        },
        preferredElement: '暂无'
      });
      
      // 显示错误提示
      wx.showToast({
        title: '未完成八字分析，请返回输入出生信息',
        icon: 'none',
        duration: 3000
      });
      
      return;
    }
    
    // 如果有命理数据，正常处理
    this.setData({
      lastName: app.globalData.nameFormData?.surname || app.globalData.birthInfo?.lastName || '未设置',
      bazi: app.globalData.destinyData.bazi,
      wuxing: app.globalData.destinyData.wuxing,
      xiyongshen: app.globalData.destinyData.xiyongshen,
      lianghua: app.globalData.destinyData.lianghua
    });
    
    // 解析量化数据格式化为图表数据
    const scores = {};
    app.globalData.destinyData.lianghua.split('，').forEach(item => {
      const parts = item.split('：');
      if (parts.length === 2) {
        scores[parts[0]] = parseFloat(parts[1]);
      }
    });
    
    this.setData({
      elementScores: scores
    });
    
    // 确定首选五行（得分最高的）
    let maxScore = -1;
    let maxElement = '';
    for (const [element, score] of Object.entries(scores)) {
      if (score > maxScore) {
        maxScore = score;
        maxElement = element;
      }
    }
    
    this.setData({
      preferredElement: maxElement
    });
  },

  showBaziExplanation: function(e) {
    const index = e.currentTarget.dataset.index;
    const bazi = this.data.destinyData.bazi[index];
    
    const explanations = {
      '甲子': '甲木得子水之助...',
      '乙丑': '乙木遇丑土...',
      // 其他解释...
    };
    
    this.setData({
      showExplanation: true,
      currentBazi: bazi,
      baziExplanation: explanations[bazi] || '解释待补充'
    });
  },

  hideExplanation: function() {
    this.setData({ showExplanation: false });
  },

  goToExpectation: function() {
    wx.navigateTo({ url: '/pages/expectation/expectation' });
  },

  // 继续按钮点击处理
  onContinue: function() {
    wx.navigateTo({
      url: '../expectation/expectation',
      fail: function(res) {
        console.error('导航失败:', res);
        wx.showToast({
          title: '导航失败: ' + res.errMsg,
          icon: 'none',
          duration: 2000
        });
      }
    });
  },
  
  // 页面分享设置
  onShareAppMessage: function() {
    return {
      title: '我的八字五行分析',
      path: '/pages/index/index'
    };
  }
})